import { Injectable, Injector, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { zip } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { MenuService, SettingsService, TitleService } from '@delon/theme';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import { ACLService } from '@delon/acl';

/**
 * 用于应用启动时
 * 一般用来获取应用所需要的基础数据等
 */
@Injectable()
export class StartupService {
  constructor(
    private menuService: MenuService,
    private settingService: SettingsService,
    private aclService: ACLService,
    private titleService: TitleService,
    @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
    private httpClient: HttpClient,
    private injector: Injector,
  ) {
  }


  private viaMock(resolve: any, reject: any) {
    // const tokenData = this.tokenService.get();
    // if (!tokenData.token) {
    //   this.injector.get(Router).navigateByUrl('/passport/login');
    //   resolve({});
    //   return;
    // }
    // mock
    const app: any = {
      name: `特伦小程序`,
      description: `特伦小程序后台管理系统`,
    };
    const user: any = {
      name: 'Admin',
      avatar: './assets/tmp/img/avatar.jpg',
      email: 'zou@qq.com',
      token: '123456789',
    };
    // 应用信息：包括站点名、描述、年份
    this.settingService.setApp(app);
    // 用户信息：包括姓名、头像、邮箱地址
    this.settingService.setUser(user);
    // ACL：设置权限为全量
    this.aclService.setFull(true);
    // 初始化菜单
    this.menuService.add([
      {
        text: '导航菜单',
        group: true,
        children: [
          {
            text: '首页',
            link: '/dashboard',
            icon: 'anticon anticon-appstore-o',
          },
          {
            text: '特伦云仓',
            icon: 'anticon anticon-share-alt',
            children: [
              {
                text: '充值申请',
                link: '/application/tabs',
                icon: 'anticon anticon-bank',
                reuse: false,
              },
              {
                text: '渠道管理',
                icon: 'anticon anticon-share-alt',
                children: [
                  {
                    text: '渠道级别',
                    link: '/agent/level-list',
                  },
                  {
                    text: '渠道列表',
                    link: '/agent/list',
                  },
                  {
                    text: '渠道详情',
                    link: '/agent/list/agent-detail',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '招市级渠道',
                    link: '/agent/director',
                  },
                  {
                    text: '晋级市级申请',
                    link: '/agent/level-application',
                  },
                  {
                    text: '升级记录',
                    link: '/agent/record',
                  },
                ],
              },
              {
                text: '渠道业绩',
                icon: 'anticon anticon-bar-chart',
                link: '/performance/list',
              },
              {
                text: '客户管理',
                icon: 'anticon anticon-team',
                children: [
                  {
                    text: '客户列表',
                    link: '/customer/list',
                  },
                  /*{
                    text: '粉丝列表',
                    link: '/customer/fans-list',
                  },*/
                ],
              },
              {
                text: '订单管理',
                icon: 'anticon anticon-bars',
                link: '/order/list',
              },
              {
                text: '资金管理',
                icon: 'anticon anticon-pay-circle-o',
                children: [
                  {
                    text: '零售记录',
                    link: '/finances/shopping',
                  },
                  {
                    text: '进货记录',
                    link: '/finances/recharge',
                  },
                  {
                    text: '出货记录',
                    link: '/finances/chargeback',
                  },
                ],
              },
              {
                text: '商品管理',
                icon: 'anticon anticon-appstore-o',
                children: [
                  {
                    text: '商品分类',
                    link: '/goods/type-list',
                  },
                  {
                    text: '商品列表',
                    link: '/goods/list',
                    reuse: false,
                  },
                  {
                    text: '商品详情',
                    link: '/goods/list/edit',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '新增商品',
                    link: '/goods/list/add',
                    hide: true,
                    reuse: false,
                  },
                ],
              },
              {
                text: '小程序首页设置',
                link: '/goods/goods-sort',
                reuse: false,
              },
              // {
              //   text: '素材管理',
              //   link: '/img-material/list/1',
              //   reuse: false,
              // },
              // {
              //   text: '素材详情',
              //   link: '/img-material/list/1/edit',
              //   hide: true,
              //   reuse: false,
              // },
            ],
          },

          {
            text: '星创之梦',
            icon: 'anticon anticon-flag',
            children: [

              {
                text: '奖金自动发放器',
                children: [
                  {
                    text: '发放器管理',
                    link: '/market-trigger/list',
                    reuse: false,
                  },
                  {
                    text: '发放统计',
                    link: '/market-trigger/statics-list',
                    reuse: false,
                  },
                  {
                    text: '统计明细',
                    link: '/market-trigger/statics-list/detail',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '发放记录',
                    link: '/market-trigger/record-list',
                    reuse: false,
                  },{
                    text: '发放器详情',
                    link: '/market-trigger/list/detail',
                    hide: true,
                    reuse: false,
                  },
                ]
              },
              {
                text: '活动管理',
                children: [
                  {
                    text: '预定产品',
                    link: '/mid-autumn/activity-product',
                    reuse: false,
                  },
                  {
                    text: '产品详情',
                    link: '/mid-autumn/activity-product/edit',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '活动类型',
                    link: '/company/company-type-list',
                  },
                  {
                    text: '活动列表',
                    link: '/company/list',
                    reuse: false,
                  },
                  {
                    text: '活动详情',
                    link: '/company/list/detail',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '小程序首页设置',
                    link: '/company/company-sort',
                    reuse: false,
                  },
                  {
                    text: '预定订单',
                    link: '/mid-autumn/cake-order',
                  },
                  {
                    text: '提现申请',
                    link: '/profit-withdraw/list',
                  },
                  // {
                  //   text: '月饼发货',
                  //   link: '/mid-autumn/cake-postal',
                  // },
                  {
                    text: '积分算力',
                    link: '/company/integral-list',
                  },
                  {
                    text: '兑换申请',
                    link: '/integral/convert',
                  },
                ],
              },
              {
                text: '出入账管理',
                children: [
                  {
                    text: '入账明细',
                    link: '/fund-flow/fund-in-list',
                  },
                  {
                    text: '出账明细',
                    link: '/fund-flow/fund-out-list',
                  },
                ],
              },
              {
                text: '账号管理',
                children: [
                  {
                    text: '营运中心',
                    link: '/salesman/district',
                  },
                  {
                    text: '升级记录',
                    link: '/salesman/role-change-record',
                  },
                  {
                    text: '星创股东列表',
                    link: '/salesman/list/1',
                  },
                  {
                    text: '星创股东详情',
                    link: '/salesman/list/1/detail',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '星创联合创始人列表',
                    link: '/salesman/list/2',
                  },
                  {
                    text: '星创联合创始人详情',
                    link: '/salesman/list/2/detail',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '星创合伙人类别',
                    link: '/salesman/salesman-type/2',
                  },
                  {
                    text: '星创合伙人列表',
                    link: '/salesman/list/4',
                  },
                  {
                    text: '星创合伙人详情',
                    link: '/salesman/list/4/detail',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '门店类别',
                    link: '/salesman/salesman-type/1',
                  },
                  {
                    text: '门店列表',
                    link: '/salesman/list/3',
                  },
                  {
                    text: '门店详情',
                    link: '/salesman/list/3/detail',
                    hide: true,
                    reuse: false,
                  },
                ],
              },
              {
                text: '轮播图管理',
                link: '/banner/list',
                reuse: false,
              },
              {
                text: '轮播图详情',
                link: '/banner/list/edit',
                hide: true,
                reuse: false,
              },
              {
                text: '用户列表',
                link: '/market-fans/list',
              },
              {
                text: '星创动态',
                link: '/market-moments/list',
              },
              {
                text: '用户感恩墙信息',
                link: '/market-fans/list/edit',
                hide: true,
                reuse: false,
              },
              {
                text: '用户业务数据记录',
                link: '/market-fans/list/detail',
                hide: true,
                reuse: false,
              },
              {
                text: '访问记录',
                link: '/views/list',
              },
              {
                text: '文章管理',
                icon: 'anticon anticon-flag',
                children: [
                  {
                    text: '品牌介绍',
                    link: '/band/list',
                    reuse: false,
                  },
                  {
                    text: '品牌介绍',
                    link: '/band/list/edit',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '积分兑换',
                    link: '/integral/list',
                    reuse: false,
                  },
                  {
                    text: '兑换产品详情',
                    link: '/integral/list/edit',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '合伙人活动',
                    link: '/activity/list',
                    reuse: false,
                  },
                  {
                    text: '活动详情',
                    link: '/activity/list/edit',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '加盟类别',
                    link: '/cars/level-list',
                  },
                  {
                    text: '加盟送车',
                    link: '/cars/list',
                    reuse: false,
                  },
                  {
                    text: '加盟送车详情',
                    link: '/cars/list/edit',
                    hide: true,
                    reuse: false,
                  },
                ],
              },
              {
                text: '意向用户',
                link: '/market-agent/wish',
                reuse: false,
              },
              {
                text: '签约用户',
                link: '/market-agent/sign',
                reuse: false,
              },
            ],
          }, {
            text: '芪枣健胃茶',
            icon: 'anticon anticon-coffee',
            children: [
              // {
              //   text: '积分规则模板',
              //   link: '/otc-rule-template/list',
              // },
              {
                text: '运营中心',
                link: '/otc-center/list',
              },
              {
                text: '运营中心详情',
                link: '/otc-center/list/detail',
                hide: true,
                reuse: false,
              },
              {
                text: '品牌规则日志',
                link: '/otc-center/list/detail/log',
                hide: true,
                reuse: false,
              },
              {
                text: '用户详情',
                link: '/otc-center/list/detail/cus/detail',
                hide: true,
                reuse: false,
              },
              {
                text: '品牌管理',
                link: '/otc-center/brand/list',
              },
              {
                text: '品牌规则日志',
                link: '/otc-center/brand/list/log',
                hide: true,
                reuse: false,
              },
              {
                text: '门店管理',
                link: '/otc-shop/list',
              },
              {
                text: '门店规则日志',
                link: '/otc-shop/list/log',
                hide: true,
                reuse: false,
              },
              {
                text: '品牌规则日志',
                link: '/otc-shop/shop-brand/log',
                hide: true,
                reuse: false,
              },
              {
                text: '用户管理',
                children: [
                  {
                    text: '用户列表',
                    link: '/otc-cus/list',
                    reuse: false,
                  },
                  {
                    text: '用户详情',
                    link: '/otc-cus/list/detail',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '票据列表',
                    link: '/otc-cus/bill-list',
                  },
                  {
                    text: 'OTC动态',
                    link: '/otc-cus/moment',
                  },
                ],
              },
              {
                text: '积分明细',
                link: '/otc-credit/list',
              },
              {
                text: '兑换管理',
                children: [
                  {
                    text: '兑换产品',
                    link: '/otc-convert/product-list',
                    reuse: false,
                  },
                  {
                    text: '兑换产品详情',
                    link: '/otc-convert/product-list/edit',
                    hide: true,
                    reuse: false,
                  },
                  {
                    text: '兑换记录',
                    link: '/otc-convert/list',
                  },
                ],
              },
              {
                text: '站内消息',
                link: '/otc-message/list',
              },
            ],
          },
          {
            text: '素材管理',
            icon: 'anticon anticon-cloud-o',
            children: [
              {
                text: '素材分类',
                link: '/img-material/material-type-list',
              },
              {
                text: '素材列表',
                link: '/img-material/list/2',
                reuse: false,
              },
              {
                text: '素材详情',
                link: '/img-material/list/2/edit',
                hide: true,
                reuse: false,
              },
            ],
          },
          // {
          //   text: '用户管理',
          //   icon: 'anticon anticon-usergroup-add',
          //   link: '/user/list'
          // },
          {
            text: '客服管理',
            icon: 'anticon anticon-customer-service',
            link: '/service-cus/list',
            reuse: false,
          },
          {
            text: '系统配置',
            icon: 'anticon anticon-setting',
            link: '/sys/params',
            reuse: false,
          },
          {
            text: '个人中心',
            icon: 'anticon anticon-user',
            link: '/curr-user',
            reuse: false,
          },
        ],
      },
    ]);
    // 设置页面标题的后缀
    this.titleService.suffix = app.name;

    resolve({});
  }

  load(): Promise<any> {
    // only works with promises
    // https://github.com/angular/angular/issues/15088
    return new Promise((resolve, reject) => {
      // http
      // this.viaHttp(resolve, reject);
      // 更好的方式是从后台请求获取 viamock 方法中的数据
      this.viaMock(resolve, reject);
    });
  }

  private viaHttp(resolve: any, reject: any) {
    zip(
      this.httpClient.get('assets/tmp/app-data.json'),
    ).pipe(
      // 接收其他拦截器后产生的异常消息
      catchError(([appData]) => {
        resolve(null);
        return [appData];
      }),
    ).subscribe(([appData]) => {

        // application data
        const res: any = appData;
        // 应用信息：包括站点名、描述、年份
        this.settingService.setApp(res.app);
        // 用户信息：包括姓名、头像、邮箱地址
        // this.settingService.setUser(res.user);
        // ACL：设置权限为全量
        this.aclService.setFull(true);
        // 初始化菜单
        this.menuService.add(res.menu);
        // 设置页面标题的后缀
        this.titleService.suffix = res.app.name;
      },
      () => {
      },
      () => {
        resolve(null);
      });
  }

}
